<template>
  <el-dialog
    title="分配角色"
    :visible="dialogVisible"
    width="30%"
    @close="closeFn"
  >
    <el-checkbox
      v-for="item in allList"
      :key="item.id"
      v-model="checkList"
      :label="item.id"
    >{{ item.name }}</el-checkbox>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeFn">取 消</el-button>
      <el-button
        type="primary"
        @click="submit"
      >确 定</el-button>
    </span>
  </el-dialog>

</template>
<script>
import { roleList } from '@/api/setting'
import { employeesInfo, assignRole } from '@/api/employees'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      checkList: [], // 选择想
      allList: [],
      userId: ''
    }
  },
  async created() {
    const res = await roleList({
      page: 1,
      pagesize: 1000
    })
    this.allList = res.data.data.rows
  },
  methods: {
    async employeesInfo(id) {
      this.userId = id
      const res = await employeesInfo(id)
      // console.log(res)
      this.checkList = res.data.data && res.data.data.roleIds || []
    },
    // 提交用户权限

    async submit() {
      await assignRole({
        id: this.userId,
        roleIds: this.checkList
      })
      this.closeFn()
    },
    // 关闭弹窗
    closeFn() {
      this.$emit('update:dialogVisible', false)
      this.checkList = []
    }

  }

}
</script>
<style>

</style>
